package com.wutianyi.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.wutianyi.entity.EntityTypeEnum.*;

import com.wutianyi.service.GeoApiServices;
import com.wutianyi.utils.NetUtils;

public class GeoApiServlet extends HttpServlet {

	private final String ALL = "all".intern();
	private final String PROVINCES = "provinces".intern();
	private final String CITYS = "citys".intern();
	private final String AREAS = "areas".intern();

	private final String DEFAULT_COUNTRY = "中国";
	private final String DEFAULT_PROVINCE = "广东省";
	private final String DEFAULT_CITY = "汕头市";

	/**
     * 
     */
	private static final long serialVersionUID = 4019708340238949977L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("application/xml;charset=utf-8");
		PrintWriter pw = resp.getWriter();
		String type = NetUtils.getParameterString(req, "type", ALL);
		if (ALL.equals(type)) {
			String country = NetUtils.getParameterString(req, Country.name(),
					DEFAULT_COUNTRY);
			pw.print(GeoApiServices.getAllGeoInformations(country));
			return;
		} else if (PROVINCES.equals(type)) {
			String country = NetUtils.getParameterString(req, Country.name(),
					DEFAULT_COUNTRY);
			pw.print(GeoApiServices.getAllProvinceInformations(country));
			return;
		} else if (CITYS.equals(type)) {
			String country = NetUtils.getParameterString(req, Country.name(),
					DEFAULT_COUNTRY);
			String province = NetUtils.getParameterString(req, Province.name(),
					DEFAULT_PROVINCE);
			pw.print(GeoApiServices.getCitysByProvince(country, province));
			return;
		} else if (AREAS.equals(type)) {
			String country = NetUtils.getParameterString(req, Country.name(),
					DEFAULT_COUNTRY);
			String province = NetUtils.getParameterString(req, Province.name(),
					DEFAULT_PROVINCE);
			String city = NetUtils.getParameterString(req, City.name(),
					DEFAULT_CITY);
			pw.print(GeoApiServices.getAreasByCity(country, province, city));
			return;
		} else {
			resp.setContentType("text/html;charset=utf-8");
			pw.println("无效参数");
		}
	}

}
